package com.naizhuai.ui.rank;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.User.UserRank;
import com.naizhuai.ui.mianbusiness.action.ActionMainLazyFragment;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.subjects.PublishSubject;

public class RankActivity extends BaseActivity {
    public static void start(Context context){
       start(context,RANK_PERSON);
    }

    public static void start(Context context,int currentstate){
        Intent intent=new Intent(context,RankActivity.class);
        intent.putExtra(RANK_STATE,currentstate);
        context.startActivity(intent);
    }
    public static final String RANK_STATE="RANK_STATE";
    /*
       人才榜
     */
    public static final int RANK_PERSON=0;
    /*
       团队榜
     */
    public static final int RANK_TEAN=1;
    /*
       组织榜
     */
    public static final int RANK_ORGANIZE=2;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.currentTime)
    TextView currentTime;
    @BindView(R.id.pager_indicator)
    MagicIndicator pagerIndicator;
    @BindView(R.id.rank_viewpager)
    ViewPager rankViewpager;
    private List<String> pagerTitle;
    private PublishSubject<String> mYearSubject = PublishSubject.create();
    private UserManageDataApiStoreIml mDataApiStoreIml;
    private OptionsPickerView pvYearOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initToolBar();
        initViewPager();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_rank;
    }


    public Observable<String> getYearSubscribe() {
        return mYearSubject;
    }

    private void initToolBar() {
        toolbar.setNavigationIcon(ContextCompat.getDrawable(this,R.drawable.ic_arrow_back_blue_24dp));
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initViewPager() {
       int  current_RankState=getIntent().getIntExtra(RANK_STATE,0);
        pagerTitle = Arrays.asList(getResources().getStringArray(R.array.tank_title));
        pagerIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return pagerTitle.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.nav_orange));
                simplePagerTitleView.setText(pagerTitle.get(index));
                simplePagerTitleView.setTextSize(13);
                simplePagerTitleView.setOnClickListener(v -> rankViewpager.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(R.color.nav_orange);
                return linePagerIndicator;
            }
        });
        pagerIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(pagerIndicator, rankViewpager);
        final List<Fragment> fragmentList = new ArrayList<>();

        fragmentList.add(TalentListFragment.newInstance());
        fragmentList.add(TeamListFragment.newInstance());
        fragmentList.add(OrganizeFragment.newInstance());
        FragmentPagerAdapter pageAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        rankViewpager.setOffscreenPageLimit(3);
        rankViewpager.setAdapter(pageAdapter);
        rankViewpager.setCurrentItem(current_RankState);
    }


    public void setYearData(List<UserRank.LabelBean> yearData) {
        currentTime.setText(yearData.get(0).label);
        pvYearOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                currentTime.setText(yearData.get(options1).label);
                mYearSubject.onNext(yearData.get(options1).key);
            }
        })
                .setTitleText(getString(R.string.quarter_selector))
                .setCancelColor(Color.GRAY)
                .setSubmitColor(getResources().getColor(R.color.lightorange))
                .setTextColorCenter(Color.LTGRAY)
                .build();
        pvYearOptions.setPicker(yearData);
    }

    @OnClick(R.id.currenttime_select)
    public void selectTime() {
        if (pvYearOptions!=null){
            pvYearOptions.show();
        }

    }
}
